package com.gitee.feizns.itrip.personal.trade.service.impl;

import com.gitee.feizns.itrip.personal.beans.pojo.HotelOrder;
import com.gitee.feizns.itrip.personal.mapper.HotelOrderMapper;
import com.gitee.feizns.itrip.personal.trade.entity.vo.ForViewConfirmOrder;
import com.gitee.feizns.itrip.personal.trade.entity.vo.ForViewCreateQcCode;
import com.gitee.feizns.itrip.personal.trade.mapper.TradeHotelOrderMapper;
import com.gitee.feizns.itrip.personal.trade.mapper.TradeHotelTempStoreMapper;
import com.gitee.feizns.itrip.personal.trade.service.TradeHotelOrderService;
import com.gitee.feizns.itrip.personal.trade.service.exception.OrderConfirmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.gitee.feizns.itrip.personal.trade.service.exception.OrderConfirmException.ORDER_IS_NOT_EXIST;
import static com.gitee.feizns.itrip.personal.trade.service.exception.OrderConfirmException.ORDER_STATUS_IS_ILLEGAL;

/**
 * @author feizns
 * @since 2019/1/13 0013
 */
@Service
public class TradeHotelOrderServiceImpl implements TradeHotelOrderService {

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private TradeHotelTempStoreMapper tradeHotelTempStoreMapper;

    @Autowired
    private TradeHotelOrderMapper tradeHotelOrderMapper;

    @Override
    public ForViewConfirmOrder toPayOrder(String orderNo) {
        ForViewConfirmOrder order = tradeHotelOrderMapper.getOrderByOrderNo(orderNo, HotelOrderMapper.ORDER_PENDING_PAYMENT_STATUS);
        if ( order == null )
            throw new OrderConfirmException(ORDER_IS_NOT_EXIST, "该订单不存在.");
        return order;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public synchronized void confirmOrder(String orderNo, String tradeNo, int payType) {
        //获取订单
        HotelOrder order = hotelOrderMapper.selectOne(new HotelOrder().setOrderNo(orderNo));

        //检查订单状态
        checkOrder(order);

        fillContent(order, tradeNo, payType);

        //修改订单状态
        hotelOrderMapper.updateById(order);

        //扣除库存
        tradeHotelTempStoreMapper.reduceInventory(order.getRoomId(), order.getCheckInDate(), order.getCheckOutDate(), order.getCount());

    }

    @Override
    public Long getOrderIdByOrderNo(String orderNo) {
        return tradeHotelOrderMapper.getOrderIdByOrderNo(orderNo);
    }

    @Override
    public HotelOrder get(String orderNo) {
        return hotelOrderMapper.selectOne(new HotelOrder().setOrderNo(orderNo));
    }

    @Override
    public ForViewCreateQcCode getOrderForViewCreateQcCode(String orderNo) {
        return tradeHotelOrderMapper.getOrderForViewCreateQcCode(orderNo);
    }

    private static void checkOrder(HotelOrder order) {
        if ( order == null )
            throw new OrderConfirmException(ORDER_IS_NOT_EXIST, "该订单不存在.");

        if ( order.getOrderStatus() != HotelOrderMapper.ORDER_PENDING_PAYMENT_STATUS )
            throw new OrderConfirmException(ORDER_STATUS_IS_ILLEGAL, "该订单已被取消或已支付");
    }


    private void fillContent(HotelOrder order, String tradeNo, int payType) {
        order.setTradeNo(tradeNo);
        order.setOrderStatus(HotelOrderMapper.ORDER_PAYMENT_SUCCESSFUL_STATUS);
        order.setPayType(payType);
        order.setModifiedBy(order.getUserId());
        order.setModifyDate(new Date());
    }

}
